var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getFare',(req,res) =>{
    var sql = $sql.fare.get
    var params = req.query
    console.log(params)
    conn.query(sql, [], function (err, result) {
        if (err) {
        console.log(err)
        }
        if (result) {
        console.log(result)
        res.send(result[0])
        }
    })
})

router.post('/editFare',(req,res) =>{
    var sql = $sql.fare.update
    var params = req.body
    params.modify_time = new Date();
    params.fare_id = parseInt(params.fare_id);
    params.touristClassPrice = parseInt(params.touristClassPrice);
    params.businessClassPrice = parseInt(params.businessClassPrice);
    params.firstClassPrice = parseInt(params.firstClassPrice);
    params.floatingRange = parseFloat(params.floatingRange);
    params.decreaseRange = parseFloat(params.decreaseRange);
    console.log(params);
    conn.query(sql, [params,params.fare_id], function (err, result) {
        if (err) {
        console.log(err)
        }
        if (result) {
        console.log(result)
        res.send(result)
        }
    })
})


module.exports = router;